//商品分类全局组件仓库
import { defineStore } from 'pinia'
import {
  reqCategory1,
  reqCategory2,
  reqCategory3,
} from '@/api/product/attr/index.ts'
import type { CategoryResponseData } from '@/api/product/attr/type.ts'
import type { CategoryState } from '@/store/modules/types/type.ts'

let useCategoryStore = defineStore('Category', {
  state: (): CategoryState => {
    return {
      category1List: [],
      category2List: [],
      category3List: [],
      category1Id: '',
      category2Id: '',
      category3Id: '',
    }
  },
  actions: {
    async getCategory1List() {
      let res: CategoryResponseData = await reqCategory1()
      console.log(res)
      if (res.code === 200) {
        this.category1List = res.data
      }
    },
    async getCategory2List() {
      let res: CategoryResponseData = await reqCategory2(this.category1Id)
      console.log('获取二级分类=》', res)
      if (res.code === 200) {
        this.category2List = res.data
      }
    },
    async getCategory3List() {
      let res: CategoryResponseData = await reqCategory3(this.category2Id)
      console.log('获取三级分类=》', res)
      if (res.code === 200) {
        this.category3List = res.data
      }
    },
  },
  getters: {},
})

export default useCategoryStore
